package com.hcinfos.gsoo.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.City;

public class CityExpandableAdapter extends BaseExpandableListAdapter {

	private List<List<City>> cityList;
	private List<String> indexList;
	private Context context;

	public CityExpandableAdapter(List<List<City>> cityList,
			List<String> indexList, Context context) {

		this.cityList = cityList;
		this.context = context;
		this.indexList = indexList;

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return cityList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {

		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = null;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.city_child, null);
		List<City> list = cityList.get(groupPosition);
		String cityName = list.get(childPosition).getName();
		TextView name_tv = (TextView) view.findViewById(R.id.city_tv);
		name_tv.setText(cityName);
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		return cityList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {

		return indexList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {

		return indexList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = null;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.city_group, null);
		}
		TextView title = (TextView) view.findViewById(R.id.index_tv);
		Log.i("wxf", indexList.get(groupPosition) + ":apha");
		title.setText(indexList.get(groupPosition));
		return view;
	}

	@Override
	public boolean hasStableIds() {

		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {

		return true;
	}

	public void setDataChange(List<List<City>> cityList, List<String> indexList) {
		this.cityList = cityList;
		this.indexList = indexList;
		notifyDataSetChanged();
	}

}
